<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>管理员列表-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/lib/jquery-2.1.3.min.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="weadmin-body">
    <div class="weadmin-block">
        <button class="layui-btn" onclick="showADD()"><i class="layui-icon"></i>添加</button>
    </div>
    <table class="layui-table">
        <colgroup>
            <col width="60">
            <col width="60">
            <col width="120">
            <col width="180">
            <col width="180">
            <col width="180">
            <col width="180">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>ID</th>
            <th>大棚号</th>
            <th>种植蔬菜类型</th>
            <th>光照(lx)</th>
            <th>温度(℃)</th>
            <th>湿度(%)</th>
            <th>操作</th>
        </thead>
        <tbody>
        <tr th:each="greenhouse : ${greenhouses}">
            <td th:id="'greenhouse_id'+${greenhouse.id}" th:text="${greenhouse.id}">1</td>
            <td th:text="'大棚' + ${greenhouse.number}">1</td>
            <td th:id="'greenhouse_vegetable'+${greenhouse.id}" th:text="${greenhouse.vegetable}">苹果</td>
            <td th:text="${greenhouse.illuminance}">admin@qq.com</td>
            <td th:text="${greenhouse.tempature}">admin</td>
            <td th:text="${greenhouse.humidity}">admin-01-01 11:11:42</td>
            <td class="td-manage">
                <!-- 编辑最适宜条件 -->
                <a th:id="'edit'+${greenhouse.id}" title="编辑" onclick="showEDIT(this.id)" href="javascript:;">
                    <i class="layui-icon">&#xe642;</i>
                </a>
                <a th:id="'del'+${greenhouse.id}" title="删除" onclick="del(this.id)" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
            </td>
        </tr>
        </tbody>
    </table>
    <div style="margin: 10px;display: none" id="showEditGS">
        <p>种植蔬菜类型</p>
        <input id="vegetable_edit" name="rfid" type="text" lay-verify="required" class="layui-input">
        <hr class="hr15">
        <input id="edit" onclick="editGS()" value="修改" lay-submit lay-filter="login" style="width:100%;" type="submit">
        <hr class="hr15">
        <input id="back_edit" onclick="back()" value="取消" lay-submit lay-filter="login" style="width:100%;"
               type="submit">
        <hr class="hr15">
        <span id="idMessage" style="display: none"></span>
    </div>
    <div style="margin: 10px;display: none" id="showAddGH">
        <p>大棚号</p>
        <input id="GHNum" name="GHNum"  type="text" lay-verify="required" class="layui-input" >
        <p>种植蔬菜类型</p>
        <input id="GHVegetable" name="GHVegetable"  type="text" lay-verify="required" class="layui-input" >
        <input id="addGH" onclick="add()" value="添加" lay-submit lay-filter="login" style="width:100%;" type="submit">
        <hr class="hr15">
        <input id="back" onclick="back()" value="取消" lay-submit lay-filter="login" style="width:100%;" type="submit">
        <hr class="hr15">
    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script src="/static/js/eleDel.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    function showEDIT(id) {
        layui.use('layer', function () {
            var layer = layui.layer;
            var greenhouse_vegetable_id = "greenhouse_vegetable" + id.substring(4);
            $("#vegetable_edit").val($("#" + greenhouse_vegetable_id).text());
            $("#idMessage").text(id.substring(4));
            layer.open({
                type: 1,
                content: $('#showEditGS')
            });
        });
    }

    function showADD() {
        layui.use('layer', function(){
            var layer = layui.layer;

            layer.open({
                type: 1,
                content: $('#showAddGH')
            });
        });
    }

    function back() {
        layui.use('layer', function () {
            var layer = layui.layer;

            layer.closeAll();
        });
    }

    function editGS() {
        var id = $("#idMessage").text();
        var vegetable = $("#vegetable_edit").val();
        if (vegetable == "" || vegetable == null || vegetable == undefined) {
            alert("请输入蔬菜类型");
        } else {
            $.ajax({
                type: "POST",
                url: "/greenhouse/editVegetable",
                data: {
                    "id": id,
                    "vegetable": vegetable
                },
                async: false,
                cache: false,
                success: function (data) {
                    if (JSON.parse(data).msg == "success") {
                        alert("修改成功");
                        window.location.reload();
                        //返回
                        back();
                    } else {
                        alert("修改失败");
                    }
                },
                error: function () {
                    alert("系统错误");
                }
            });
        }
    }

    function add() {
        var GHNum = $("#GHNum").val();
        var GHVegetable = $("#GHVegetable").val();
        if (GHNum == "" || GHNum == null || GHNum == undefined ||
            GHVegetable == "" || GHVegetable == null || GHVegetable == undefined){
            alert("请输入完整数据");
        } else {
            $.ajax({
                type: "POST",
                url: "/greenhouse/addGreenHouse",
                data: {
                    "number": GHNum,
                    "vegetable": GHVegetable
                },
                async: false,
                cache: false,
                success: function (data) {
                    if (JSON.parse(data).msg == "success") {
                        alert("添加成功,请前往传感器列表页面添加串口");
                        window.location.reload();
                        //返回
                        back();
                    } else {
                        alert("添加失败");
                    }
                },
                error: function () {
                    alert("系统错误");
                }
            });
        }
    }

    function del(id) {
        var house_id = id.substring(3);
        var con = confirm('确认删除大棚 ' + house_id + ' 吗? 该操作同时会删除该大棚中的所有串口');
        if (con == true) {
            $.ajax({
                type: "POST",
                url: "/greenhouse/delGreenHouse",
                data: {
                    "house_id": house_id
                },
                async: false,
                cache: false,
                success: function (data) {
                    if (JSON.parse(data).msg == "success") {
                        alert("删除成功");
                        window.location.reload();
                        //返回
                        back();
                    } else {
                        alert("删除失败");
                    }
                },
                error: function () {
                    alert("系统错误");
                }
            });
        }else {

        }
    }

</script>
</body>
</html>